# -*- coding:Utf-8 -*-
'''
Created on Dec 7, 2012

@author: fefoley
'''

from pyicl import  Interval, IntervalMap, IntervalStringMap, IntervalSet, IntIntervalSet, IntInterval, IntervalBounds
from datetime import timedelta, date

from divers import Str2sum
import myio

if __name__=="__main__":
    
    # file constantes - could be automated using an explorer to choose file
    input_root_path = "/home/test-dev/reverse_hours.io/"
    path_to_configfile = input_root_path + "reverse_hours.config.txt"
        
    "importation des paramètres de config"
    config = myio.import_config(path_to_configfile)
    
    "importation des données dans des listes"
    #lstmails = myio.import_mails(config) # importer la liste des mails et retourne une liste d'objet Mail
    #lstmeetings = myio.import_meetings(config) # importer la liste des réunions et retourne une liste d'objet Meeting
#    lstsentmeetings = myio.import_sentmeetings(config)
#    
#    lstsentmeetings = lstsentmeetings[100:110]
    
    
    def compute_we(scope_interval):
        start = scope_interval.lower
        end = scope_interval.upper
        we_map = IntervalStringMap()
        while start != end:
            if start.weekday() == 5:    # 5 = saturday
#                we_map += IntervalMap.Segment(Interval(start), Str2sum("WE-Samedi"))
                we_map += IntervalStringMap.Segment(Interval(start), "WE-Samedi")
            elif start.weekday() == 6:   # 6 = synday
#                we_map += IntervalMap.Segment(Interval(start), Str2sum("WE-Dimanche"))        
                we_map += IntervalStringMap.Segment(Interval(start), "WE-Dimanche")        

            start += timedelta(days=1)
        return we_map
        
    
    scope = myio.import_scope(config)
    we_map = compute_we(scope)         
    holidays_map = myio.import_holidays2(config).intersection(scope)
    daysoff_map = myio.import_daysoff2(config).intersection(scope)

    alloff_map = holidays_map + daysoff_map + we_map
    scope_map = IntervalStringMap(IntervalStringMap.Segment(scope, "scope"))
    
    
    print(alloff_map)
    print('--------')
    print(scope_map)
    print('--------')
    diff_map = scope_map ^ alloff_map
#    print(IntervalMap(IntervalMap.Segment(scope, Str2sum(""))) ^ alloff_map)
    print(diff_map)
    
    
    myset = IntIntervalSet()
    for s in diff_map:
        myset += IntInterval(s.interval.lower.toordinal(), s.interval.upper.toordinal(), s.interval.bounds)
    
    print("--------------")    
    print("myset= " + str(myset))
    
    myset2 = IntervalSet()
    for i in myset:
        bb = {str(IntervalBounds.OPEN): (1,-1), str(IntervalBounds.CLOSED): (0,0), 
              str(IntervalBounds.LEFT_OPEN): (1,0), str(IntervalBounds.RIGHT_OPEN): (0,-1)}
        mod1, mod2 = bb[str(i.bounds)]   
        myset2 += Interval(date.fromordinal(i.lower + mod1), date.fromordinal(i.upper + mod2), IntervalBounds.CLOSED) 
    
    print("--------------")    
    print("myset2= " + str(myset2))    
    
#    hours_ON_set = compute_hours_ON()
#    hours_OFF_set = compute_hours_OFF()
    
    
    
#    
    
    
    